package it.controllocredito.frontend.fragments;

import java.util.ArrayList;
import java.util.List;

import it.controllocredito.R;
import it.controllocredito.backend.persistence.CalllogPersistenceManager;
import it.controllocredito.backend.persistence.bean.RegistroChiamate;
import it.controllocredito.frontend.util.RegistroChiamateArrayAdapter;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.support.v4.app.Fragment;


public class CallTabFragment extends Fragment implements OnScrollListener
{

	private static final int CALLS_BLOCK_SIZE = 15;
	private static final String	log_class_name	= "CallTabFragment";
	private RegistroChiamateArrayAdapter registroChiamateArrayAdapter;
	private List<RegistroChiamate> listaRegistroChiamate;
	private CalllogPersistenceManager calllogPersistenceManager;
	private ArrayList<RegistroChiamate> x;
	private int lastBlockLoaded;
	private int totalCallItems;
	private ListView callsListView;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) 
	{
		return inflater.inflate(R.layout.call_tab_fragment, group, false);		
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		initFragment(this.getActivity().getApplicationContext());
	}
	
	public void initFragment(Context appContext){
		calllogPersistenceManager = new CalllogPersistenceManager(appContext);	
		listaRegistroChiamate = calllogPersistenceManager.getRecordsRegistroChiamate(1, CALLS_BLOCK_SIZE);
		totalCallItems = calllogPersistenceManager.getTotalRecordsRegistroChiamate();
		
		x = new ArrayList<RegistroChiamate>();
		lastBlockLoaded=1;
		for(int i = 0; i<listaRegistroChiamate.size();i++){	
			x.add(listaRegistroChiamate.get(i));
		}
		
		registroChiamateArrayAdapter = new RegistroChiamateArrayAdapter(appContext, x);

		callsListView = (ListView) getView().findViewById(R.id.mylist);

		// Assign adapter to ListView
		callsListView.setAdapter(registroChiamateArrayAdapter);
		// Assign onScroll listener
		callsListView.setOnScrollListener(this);
	}
	
	public void updateFragment(Context appContext){
		calllogPersistenceManager = new CalllogPersistenceManager(appContext);	
		listaRegistroChiamate = calllogPersistenceManager.getRecordsRegistroChiamate(1, CALLS_BLOCK_SIZE);
		totalCallItems = calllogPersistenceManager.getTotalRecordsRegistroChiamate();
		
		x = new ArrayList<RegistroChiamate>();
		lastBlockLoaded=1;
		for(int i = 0; i<listaRegistroChiamate.size();i++){	
			x.add(listaRegistroChiamate.get(i));
		}

		registroChiamateArrayAdapter = new RegistroChiamateArrayAdapter(appContext, x);
		if(callsListView!=null){
			// Assign adapter to ListView
			callsListView.setAdapter(registroChiamateArrayAdapter);
			// Assign onScroll listener
			callsListView.setOnScrollListener(this);
		} 
		
		
	}
		
	public void onScroll(AbsListView view,
        int firstVisible, int visibleCount, int totalCount) {

        boolean loadMore = (((firstVisible + visibleCount) >= (lastBlockLoaded*CALLS_BLOCK_SIZE)) && ((lastBlockLoaded*CALLS_BLOCK_SIZE)+1<totalCallItems));

        if(loadMore) {
        	listaRegistroChiamate.clear();
        	lastBlockLoaded++;
        	listaRegistroChiamate = calllogPersistenceManager.getRecordsRegistroChiamate(lastBlockLoaded, CALLS_BLOCK_SIZE);
        	for(int i = 0; i<listaRegistroChiamate.size();i++){	
    			x.add(listaRegistroChiamate.get(i));
    		}
            registroChiamateArrayAdapter.notifyDataSetChanged();
        }
    }

    public void onScrollStateChanged(AbsListView v, int s) { }   
	
}
